package org.jeecg.modules.xl.wxbot.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiModelProperty;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.xl.wxbot.entity.XlWxbotGroup;
import org.jeecg.modules.xl.wxbot.entity.XlWxbotGroupMember;
import org.jeecg.modules.xl.wxbot.mapper.XlWxbotGroupMapper;
import org.jeecg.modules.xl.wxbot.service.IXlWxbotGroupMemberService;
import org.jeecg.modules.xl.wxbot.service.IXlWxbotGroupService;
import org.jeecg.modules.xl.wxbot.vo.*;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: xl_wxbot_group
 * @Author: jeecg-boot
 * @Date:   2025-02-12
 * @Version: V1.0
 */
@Service
public class XlWxbotGroupServiceImpl extends ServiceImpl<XlWxbotGroupMapper, XlWxbotGroup> implements IXlWxbotGroupService {

    @Autowired
    private IXlWxbotGroupMemberService xlWxbotGroupMemberServer;
    @Value("${qianxunAPiBaseURL}")
    private String qianxunAPiBaseURL;
    @Value("${botWxid}")
    private String botWxid;
    @Override
    public Result<?> reflashWxBotData() {

        String apiUrl = qianxunAPiBaseURL+botWxid;
        QianxunBaseReq qianxunBaseReq = new QianxunBaseReq();
        qianxunBaseReq.setType("Q0006");
        Map dataMap = new HashMap();
        dataMap.put("type",2);//1=从缓存中获取，2=重新遍历二叉树并刷新缓存
        qianxunBaseReq.setData(dataMap);
        String resultJson = HttpUtil.post(apiUrl, JSON.toJSONString(qianxunBaseReq));
        GetGroupListResp getGroupListResp = JSON.parseObject(resultJson, GetGroupListResp.class);
        List<GroupData> groupDataList = getGroupListResp.getResult();
        for(GroupData groupData :groupDataList){
            XlWxbotGroup entity = new XlWxbotGroup();
            entity.setWxid(botWxid);
            /**群聊id*/
            entity.setGroupWxid(groupData.getWxid());
            /**群聊昵称*/
            entity.setNick(groupData.getNick());
            /**群成员数*/
            entity.setGroupMemberNum(groupData.getGroupMemberNum());
            /**群主微信id*/
            entity.setGroupManger(groupData.getGroupManger());
            entity.setAvatarMinUrl(groupData.getAvatarMinUrl());
            try{
                super.save(entity);
            }catch (DuplicateKeyException e){
                continue;

            }

            getGroupMem(groupData.getWxid());
            //获取群成员

        }
        return Result.ok("更新成功");
    }

    /**
     * @api https://apifox.com/apidoc/shared-d9199761-09b0-4ccb-8ee3-4b4aef009981/api-188929108

     * @param groupId
     */
    private void getGroupMem(String groupId){

        String apiUrl = qianxunAPiBaseURL+botWxid;
        QianxunBaseReq qianxunBaseReq = new QianxunBaseReq();
        qianxunBaseReq.setType("Q0008");
        Map dataMap = new HashMap();
        dataMap.put("type",2);//1=从缓存中获取，2=重新遍历二叉树并刷新缓存
        dataMap.put("wxid",groupId);//群wxid
        dataMap.put("getNick",2);//1=不获取昵称，2=获取群昵称（性能影响）
        qianxunBaseReq.setData(dataMap);
        String resultJson = HttpUtil.post(apiUrl, JSON.toJSONString(qianxunBaseReq));
        System.out.println(resultJson);
        GetGroupMemResp getGroupMemResp = JSON.parseObject(resultJson, GetGroupMemResp.class);
        List<GroupMem> groupMemList = getGroupMemResp.getResult();
        for(GroupMem groupMem :groupMemList){
            XlWxbotGroupMember entity = new XlWxbotGroupMember();
            WxObjectInfo memInfo = getObejctInfo(groupMem.getWxid());
             entity.setWxid(botWxid);
            /**群聊id*/
            entity.setGroupWxid(groupId);
            /**群成员wxid*/
            entity.setMemWxid(memInfo.getWxid());
            /**群成员群昵称*/
            entity.setMemGroupNick(groupMem.getGroupNick());
            entity.setMemNick(memInfo.getNick());
            /**头像小图*/
            entity.setMemAvatarMinUrl(memInfo.getAvatarMaxUrl());
            try{
                xlWxbotGroupMemberServer.saveOrUpdate(entity);
            }catch (DuplicateKeyException e){
                continue;
            }

        }
    }

    private WxObjectInfo getObejctInfo(String wxid){
        String apiUrl = qianxunAPiBaseURL+botWxid;
        QianxunBaseReq qianxunBaseReq = new QianxunBaseReq();
        qianxunBaseReq.setType("Q0004");
        Map dataMap = new HashMap();
        dataMap.put("type",2);//1=从缓存中获取，2=重新遍历二叉树并刷新缓存
        dataMap.put("wxid",wxid);//wxid
        qianxunBaseReq.setData(dataMap);
        String resultJson = HttpUtil.post(apiUrl, JSON.toJSONString(qianxunBaseReq));
        GetWxInfoResp getWxInfoResp = JSON.parseObject(resultJson, GetWxInfoResp.class);
        return getWxInfoResp.getResult();
    }

}
